//
//  LGPhotoReportCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/8.
//

#import "LGPhotoReportCell.h"
#import "LGCheckPhotoTool.h"
@interface LGPhotoReportCell()

/**   */
@property (nonatomic , strong)UIView *lineView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UIButton *checkBtn;
/**   */
@property (nonatomic , strong)UIView *photoView;
/**   */
@property (nonatomic , strong)UIButton *countButton;
/**   */
@property (nonatomic , assign)CGFloat photoW;
/**   */
@property (nonatomic , strong)NSMutableArray *picArray;
/**   */
@property (nonatomic , strong)NSMutableArray *urlArray;
/**   */
@property (nonatomic , strong)NSMutableArray *fileArray;
/**   */
@property (nonatomic , strong)NSMutableArray *buttonArray;

@end

@implementation LGPhotoReportCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        NSInteger count = isPad?8:4;
        self.photoW = (CGFloat)(Screen_W-viewPix(63)-viewPix(8)*(count-1))/count;
        [self createSubView];
        
    }
    return  self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    self.titleLabel.text = [NSString stringWithFormat:@"%@ 上传",dataDic[@"date"]];
    [self.checkBtn setTitle:LGNSString(dataDic[@"type_name"]) forState:UIControlStateNormal];
    [self.checkBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
    NSArray *itemArray = dataDic[@"items"];
    NSMutableArray *imageArray = [NSMutableArray array];
    NSMutableArray *fileArray = [NSMutableArray array];
    for (NSDictionary *tempDic in itemArray) {
        if ([tempDic.allKeys containsObject:@"imgid"]) {
            //图片
            [imageArray addObject:tempDic];
        }else{
            //文件
            [fileArray addObject:tempDic];
        }
    }
    self.fileArray = fileArray;
    [self createPhotoView:imageArray];
    [self createFileView:fileArray];
    CGFloat height = imageArray.count>0?(viewPix(22)+self.photoW):viewPix(6);
    height += viewPix(32)*fileArray.count+viewPix(6);
    
    [self.photoView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height));
    }];
}


-(void)checkFileAction:(UIButton *)sender{
    NSInteger index = sender.tag-2000;
    if (self.delegate && [self.delegate respondsToSelector:@selector(checkFileDetail:)]) {
        [self.delegate checkFileDetail:self.fileArray[index]];
    }
}

-(void)createPhotoView:(NSArray *)photoArray{
    for (UIImageView *imageView in self.picArray) {
        [imageView removeFromSuperview];
    }
    [self.picArray removeAllObjects];
    
    self.urlArray = [NSMutableArray array];
    NSInteger maxCount = isPad?8:4;
    NSInteger count = (photoArray.count>maxCount)?maxCount:photoArray.count;
    for (NSInteger i=0; i<photoArray.count; i++) {
        NSString *urlStr = LGNSString(photoArray[i][@"url"]);
        [self.urlArray addObject:urlStr];
        if(i<count){
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(viewPix(12)+(self.photoW+viewPix(8))*i, viewPix(16), self.photoW, self.photoW)];
            [imageView sd_setImageWithURL:[NSURL URLWithString:urlStr]];
            imageView.userInteractionEnabled = YES;
            imageView.cornerRidus = 3;
            imageView.tag = 1000+i;
            [imageView addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(checkBigPhotoAction:)]];
            [self.photoView addSubview:imageView];
            [self.picArray addObject:imageView];
        }
    }
    
    self.countButton.hidden = (photoArray.count>maxCount)?NO:YES;
    [self.countButton setTitle:[NSString stringWithFormat:@"+%ld",photoArray.count-4] forState:UIControlStateNormal];
    self.countButton.frame = CGRectMake(viewPix(12)+(self.photoW+viewPix(8))*(maxCount-1), viewPix(16), self.photoW, self.photoW);
    [self.photoView bringSubviewToFront:self.countButton];
}

-(void)createFileView:(NSArray *)fileArray{
    for (UIButton *button in self.buttonArray) {
        [button removeFromSuperview];
    }
    [self.buttonArray removeAllObjects];
    
    CGFloat top = (self.picArray.count>0)?self.photoW+viewPix(22):viewPix(6);
    for (NSInteger i=0; i<fileArray.count; i++) {
        NSDictionary *tempDic = fileArray[i];
        UIButton *button = [UIButton buttonWithTitle:LGNSString(tempDic[@"title"]) titleFont:LGFont(15) textColor:@"#333333" frame:CGRectMake(viewPix(12), top+viewPix(32)*i, Screen_W-viewPix(56), viewPix(32))];
        button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        button.tag = 2000+i;
        [button addTarget:self action:@selector(checkFileAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.photoView addSubview:button];
        [self.buttonArray addObject:button];
        UIImageView *tipView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"grayTip16"]];
        [button addSubview:tipView];
        [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(button);
            make.width.height.equalTo(@(16));
            make.centerY.equalTo(button);
        }];
    }
    
}

-(void)countButtonAction{
    LGCheckPhotoTool *bigPhotoView = [[LGCheckPhotoTool alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    bigPhotoView.title = @"图片报告";
    bigPhotoView.picArray = self.urlArray;
    [bigPhotoView showViewAnimation:CGPointMake(0, 0) index:0];
    [[UIApplication sharedApplication].keyWindow addSubview:bigPhotoView];
}

-(void)checkBigPhotoAction:(UITapGestureRecognizer *)sender{
    UIImageView *imageView = (UIImageView *)sender.view;
    NSInteger index = imageView.tag-1000;
    LGCheckPhotoTool *bigPhotoView = [[LGCheckPhotoTool alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    bigPhotoView.title = @"图片报告";
    bigPhotoView.picArray = self.urlArray;
    [bigPhotoView showViewAnimation:CGPointMake(0, 0) index:index];
    [[UIApplication sharedApplication].keyWindow addSubview:bigPhotoView];
}

-(void)checkMorePhotoAction{
    if(self.delegate && [self.delegate respondsToSelector:@selector(checkPhotoReportDetail:)]){
        [self.delegate checkPhotoReportDetail:self.dataDic];
    }
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.clipsToBounds = YES;
    self.backgroundColor = [UIColor clearColor];
    [self.contentView addSubview:self.lineView];
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.checkBtn];
    [self.contentView addSubview:self.photoView];
    [self.photoView addSubview:self.countButton];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineView.mas_right).offset(viewPix(12));
        make.centerY.equalTo(self.lineView);
    }];
    
    [self.photoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView).offset(viewPix(42));
        make.left.equalTo(self.contentView).offset(viewPix(16));
        make.right.equalTo(self.contentView).offset(-viewPix(16));
        make.bottom.equalTo(self.contentView).offset(-viewPix(16));
        make.height.equalTo(@(viewPix(104)));
    }];

}
-(NSMutableArray *)picArray{
    if(!_picArray){
        _picArray = [NSMutableArray array];
    }
    return _picArray;
}

-(NSMutableArray *)fileArray{
    if(!_fileArray){
        _fileArray = [NSMutableArray array];
    }
    return _fileArray;
}

-(NSMutableArray *)buttonArray{
    if(!_buttonArray){
        _buttonArray = [NSMutableArray array];
    }
    return _buttonArray;
}

-(UIView *)lineView{
    if(!_lineView){
        _lineView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(18), 4, viewPix(14))];
        _lineView.backgroundColor = [UIColor colorWithString:@"#77D4C6"];
        _lineView.cornerRidus = 2;
    }
    return _lineView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(14, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
    }
    return _titleLabel;
}

-(UIButton *)checkBtn{
    if(!_checkBtn){
        _checkBtn = [UIButton buttonWithTitle:@"查看" titleFont:LGFont(14) textColor:@"#666666" imageName:@"grayTip12"];
        _checkBtn.frame = CGRectMake(Screen_W-viewPix(166), viewPix(6), viewPix(150), viewPix(40));
        _checkBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_checkBtn addTarget:self action:@selector(checkMorePhotoAction) forControlEvents:UIControlEventTouchUpInside];
        [_checkBtn setButtonInsetsStyle:LGButtonInsetsStyleRight marginSpace:3];
    }
    return _checkBtn;
}

-(UIView *)photoView{
    if(!_photoView){
        _photoView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(42), Screen_W-viewPix(32), viewPix(104))];
        _photoView.backgroundColor = [UIColor whiteColor];
        _photoView.cornerRidus = 15;
    }
    return _photoView;
}

-(UIButton *)countButton{
    if(!_countButton){
        _countButton = [UIButton buttonWithTitle:@"#FFFFFF" titleFont:LGFontWeight(22, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _countButton.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
        _countButton.cornerRidus = 3;
        [_countButton addTarget:self action:@selector(checkMorePhotoAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _countButton;
}


@end
